package com.xuecheng.content.contrller;



import com.xuecheng.base.execption.ValidationGroups;
import com.xuecheng.content.utils.SecurityUtil;
import com.xuecheng.content.utils.XcUser;
import com.xuecheng.model.PageParams;
import com.xuecheng.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.service.iml.CourseBaseServiceIml;
import com.xuecheng.content.model.po.CourseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2023/2/11 15:44
 */
@Api(value = "课程信息管理接口",tags = "课程信息管理接口")
@RestController
@RequiredArgsConstructor
@Slf4j
public class CourseBaseInfoController {
    private final CourseBaseServiceIml courseBaseServiceIml;

    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    //xc_teachmanager_course_list
    @PreAuthorize("hasAnyAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody QueryCourseParamsDto queryCourseParams){
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //用户所属机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }

        PageResult<CourseBase> pageResult = courseBaseServiceIml.queryCourseBaseList(companyId,pageParams, queryCourseParams);
        log.info("课程查询接口查询到的信息：{}",pageResult);
        return pageResult;
    }


    @ApiOperation("新增课程接口")
    @PostMapping("/course")
    //@Validated开启JSR303检验
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class}) AddCourseDto addCourseDto){
        log.info("新增课程接口");
        //TODO 公司ID固定
        Long companyId = 1232141425L;
        CourseBaseInfoDto courseBase = courseBaseServiceIml.createCourseBase(companyId,addCourseDto);
        return courseBase;
    }

    @ApiOperation("课程id查询基本信息接口")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        log.info("课程id查询基本信息接口");
        return courseBaseServiceIml.getCourseBaseById(courseId);
    }

    @ApiOperation("修改课程接口")
    @PutMapping("/course")
    public CourseBaseInfoDto modifCourseBase(@RequestBody @Validated({ValidationGroups.Update.class}) EditCourseDto editCourseDto){
        log.info("修改课程接口");
        //TODO 公司ID固定
        Long companyId = 1232141425L;
        return courseBaseServiceIml.modifCourseBase(companyId,editCourseDto);
    }
    @ApiOperation("删除课程接口")
    @DeleteMapping("/course/{id}")
    public void deleteCourseBase(@PathVariable Long id){
        log.info("删除课程");
        courseBaseServiceIml.deleteCourseBase(id);
    }
}
